package com.zte.shopping.service.impl;


import com.zte.shopping.action.backend.BackendLoginController;
import com.zte.shopping.constant.StatusConstant;
import com.zte.shopping.entity.ProductType;
import com.zte.shopping.exception.ProductTypeExistException;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.mapper.productType.ProductTypeMapper;
import com.zte.shopping.service.ProductTypeService;
import com.zte.shopping.util.Constants;
import com.zte.shopping.util.ParameterUtil;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: ssm
 * @description: 产品类型service实现
 * @author: zuti
 * @create: 2020-06-28 14:22
 **/

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    private  static  final Logger logger= Logger.getLogger(ProductTypeServiceImpl.class);
    @Autowired
    ProductTypeMapper productTypeMapper;

    @Override
    public List<ProductType> allProductTypeList() {
        return productTypeMapper.allProductTypeList();
    }

    @Override
    public List<ProductType> productTypeList( Integer pageIndex) {

        int pageSize = Constants.pageSize;
        int from = (pageIndex-1)*pageSize;
        logger.debug("enter AdminServiceImpl ------> productTypeList param: pageIndex="+pageIndex+" pageSize="+pageSize+" from="+from);
        return productTypeMapper.productTypeList(from,pageSize);
    }

    @Override
    public int productTypeListCount() {
        logger.debug("enter ProductTypeServiceImpl ------> productTypeListCount()");
        return productTypeMapper.productTypeListCount();
    }

    @Override
    public void modifyStatus(String id, String status) {
        int typeStatus = Integer.parseInt(status);

        if (typeStatus == StatusConstant.PRODUCT_TYPE_STATUS_DISABLE) // 禁用   ---> 启用
        {
            typeStatus = StatusConstant.PRODUCT_TYPE_STATUS_ENABLE;
        }else
        {
            typeStatus = StatusConstant.PRODUCT_TYPE_STATUS_DISABLE;  // 启用  ---> 禁用
        }

        // 根据 商品类型的id  修改  商品类型状态
        productTypeMapper.updateStatus(Integer.parseInt(id), typeStatus);
    }

    /**
     * 修改 商品类型的名称
     * 修改时,商品类型的名称不能与数据库中其他名称重复
     * 如果商品类型的名称不是原来的且数据库总已经存在了,则抛出异常
     *
     */
    @Override
    public void modifyName(String id, String name) throws ProductTypeExistException {

        // 当新的类型名称在DB中不存在,则可以修改
        ProductType pt1 = productTypeMapper.selectByName(name);

        // 存在该名称,抛出异常
        if ( pt1 != null)
        {
            throw new ProductTypeExistException("该类型名称已经存在!");
        }

        // 根据 商品类型的id  修改  商品类型名称
        productTypeMapper.updateName(Integer.parseInt(id), name);
    }

    @Override
    public void addType(String name) throws ProductTypeExistException, RequestParameterException {
        if (ParameterUtil.isnull(name))
        {
            throw new RequestParameterException("商品类型名称不能为空");
        }

        ProductType selectType = productTypeMapper.selectByName(name);

        if (selectType != null)
        {
            throw new ProductTypeExistException("该商品类型已经存在");
        }

        productTypeMapper.insertProductType(name, StatusConstant.PRODUCT_TYPE_STATUS_ENABLE);

    }

    @Override
    public List<ProductType> findEnableProductTypeList() {

        return productTypeMapper.findEnableProductTypeList();
    }


}
